package com.example.action;

import com.example.bean.Page;
import com.example.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/productList")
public class ProductListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = Integer.parseInt(req.getParameter("pageNo"));
        String searchQuery = req.getParameter("search");

        ProductService service = new ProductService();
        Page page;
        if (searchQuery != null && !searchQuery.trim().isEmpty()) {
            // 根据搜索条件查询商品
            page = service.searchProductsByQuery(searchQuery, pageNo, 9);
        } else {
            // 查询所有商品
            page = service.selectAllProductByPage(pageNo, 9);
        }
        req.setAttribute("plist", page.getList());
        req.setAttribute("page", page); // 将 Page 对象传递到 JSP
        req.setAttribute("search", searchQuery);
        req.getRequestDispatcher("shop.jsp").forward(req, resp);
    }
}